package mo.offer_version2;

import java.util.LinkedList;
import java.util.Queue;

public class L59_2 {

    private LinkedList<Integer> max = new LinkedList<>();
    private Queue<Integer> queue = new LinkedList<>();

    public L59_2() {

    }

    public int max_value() {
        if (max.isEmpty()) {
            return -1;
        }
        return max.peek();
    }

    public void push_back(int value) {
        queue.add(value);
        if (max.isEmpty()) {
            max.add(value);
        } else {
            while (!max.isEmpty() && max.peekLast() < value) {
                max.pollLast();
            }
            max.add(value);
        }
    }

    public int pop_front() {
        if (queue.isEmpty()) {
            return -1;
        }
        int val = queue.poll();
        if (val == max.peek()) {
            max.poll();
        }
        return val;
    }

}
